package com.cy.store.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor{
    /**
     * 检查全局session数据是否有uid，有则放行，没有重定向登录界面
     * @param request
     * @param response
     * @param handler 处理器（url+controller映射）
     * @return 返回true放行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object obj = request.getSession().getAttribute("uid");
        if(obj == null){
             //未登录则重定向
            response.sendRedirect("/web/login.html");
            return false;
        }
        return true;
    }
}
